package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.dto.common.plan.PlanDangerDto;
import com.zjuee.service.PlanDangerService;
import com.zjuee.base.model.common.plan.PlanDanger;
import com.zjuee.web.ledger.mapper.PlanDangerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ding jinli
 * @since 1.0
 */
@Slf4j
@Service
public class PlanDangerServiceImpl implements PlanDangerService {
    @Resource
    private PlanDangerMapper planDangerMapper;

    @Override
    public Long add(PlanDanger type) {
        int state = planDangerMapper.insert(type);
        if (1 == state) {
            return type.getId();
        }
        return null;
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        PlanDanger planDanger = planDangerMapper.selectById(id);
        if (validateId.equals(planDanger.getWpiId())) {
            int type = planDangerMapper.deleteById(id);
            if (1 == type) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean update(PlanDanger type) {
        int state = planDangerMapper.updateById(type);
        if (1 == state) {
            return true;
        }
        return false;
    }

    @Override
    public PlanDanger selectById(Long id, Long validateId) {
        PlanDanger planDanger = planDangerMapper.selectById(id);
        if (validateId.equals(planDanger.getWpiId())) {
            return planDanger;
        }
        return null;
    }

    @Override
    public Page<PlanDanger> list(PlanDangerDto dto) {
        return null;
    }

    @Override
    public LambdaQueryWrapper<PlanDanger> fuzzyQuery(PlanDangerDto dto) {
        return null;
    }

    @Override
    public List<PlanDanger> finDangerListBywpiId(Long wpiId, Integer wpdType) {
        if (wpiId == null || wpdType == null) {
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("wpi_id", wpiId);
        queryWrapper.eq("wpd_type", wpdType);
        return planDangerMapper.selectList(queryWrapper);
    }

    @Override
    public List<String> getScodeByWpiId(Long planId, String wpdType) {
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        if (wpdType.equals("11")) {
            map.put("wpi_id", planId);
            map.put("wpd_model", 1);
            map.put("wpd_type", wpdType);
            for (PlanDanger planDanger : planDangerMapper.selectByMap(map)) {
                list.add(planDanger.getWpdScode());
            }
        } else {
            list = planDangerMapper.getMedicalScode();
        }
        return list;
    }

    @Override
    public List<String> getNameByScode(String scode, Long wpiId, String wpdType) {
        Map<String, Object> map = new HashMap<>();
        map.put("wpd_scode", scode);
        map.put("wpi_id", wpiId);
        map.put("wpd_model", 1);
        map.put("wpd_type", wpdType);
        List<String> list = new ArrayList<>();
        for (PlanDanger planDanger : planDangerMapper.selectByMap(map)) {
            list.add(planDanger.getWpdName());
        }
        return list;
    }

    @Override
    public List<PlanDanger> getPlanDanger(PlanDanger planDanger) {
        if (planDanger == null) {
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        if (planDanger.getWpiId() != null) {
            queryWrapper.eq("wpi_id", planDanger.getWpiId());
        }
        if (planDanger.getWpdModel() != null) {
            queryWrapper.eq("wpd_model", planDanger.getWpdModel());
        }
        if (planDanger.getWpdType() != null) {
            queryWrapper.eq("wpd_type", planDanger.getWpdType());
        }
        if (planDanger.getWpdBcode() != null) {
            queryWrapper.eq("wpd_bcode", planDanger.getWpdBcode());
        }
        if (planDanger.getWpdScode() != null) {
            queryWrapper.eq("wpd_scode", planDanger.getWpdScode());
        }
        if (planDanger.getWpdName() != null) {
            queryWrapper.eq("wpd_name", planDanger.getWpdName());
        }

        return planDangerMapper.selectList(queryWrapper);
    }

    @Override
    public String getFeatureByScode(String scode) {
        return planDangerMapper.getFeatureByScode(scode);
    }

}
